package dao

import (
	"context"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"mall-portalv2/global"
	"mall-portalv2/internal/model"
)

func (d *Dao) UpdateCrowdFundingThali(ctx context.Context, ID int64, upadtes map[string]interface{}) error {
	tb := model.SmsCrowdFundingThali{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("id = ?", ID).Updates(upadtes).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) LockStockAddOne(ctx context.Context, ProductId, ProductSkuId, CrowdFundingId, CrowdFundingThaliId int64, num int64) error {
	if err := d.db.Transaction(func(tx *gorm.DB) error {

		tb := model.ViewProductCrowdFundingDetail{}
		result := model.ViewProductCrowdFundingDetail{}

		if err := tx.WithContext(ctx).Table(tb.TableName()).Where("crowd_funding_id = ? AND crowd_funding_thali_id = ? AND product_sku_id = ? AND product_id = ?", CrowdFundingId, CrowdFundingThaliId, ProductSkuId, ProductId).First(&result).Error; err != nil {
			return err
		}

		global.Log.Info("查询完毕", zap.Any("data", result))

		tb2 := model.PmsSkuStock{}
		skuUpdates := make(map[string]interface{})
		skuUpdates["lock_stock"] = result.SkuLockStock + num
		if err := tx.WithContext(ctx).Table(tb2.TableName()).Where("id = ?", ProductSkuId).Updates(skuUpdates).Error; err != nil {
			return err
		}

		global.Log.Info("商品表库存更新")

		tb3 := model.SmsCrowdFundingThali{}
		crowdFundingUpdates := make(map[string]interface{})
		crowdFundingUpdates["lock_thali_count"] = result.CftLockThaliCount + num
		if err := tx.WithContext(ctx).Table(tb3.TableName()).Where("id = ?", CrowdFundingThaliId).Updates(crowdFundingUpdates).Error; err != nil {
			return err
		}

		global.Log.Info("众筹表库存更新")

		// 返回 nil 提交事务
		return nil
	}); err != nil {
		return err
	}
	return nil
}

func (d *Dao) LockStockReduceOne(ctx context.Context, ProductId, ProductSkuId, CrowdFundingId, CrowdFundingThaliId int64, num int64) error {
	if err := d.db.Transaction(func(tx *gorm.DB) error {

		tb := model.ViewProductCrowdFundingDetail{}
		result := model.ViewProductCrowdFundingDetail{}

		if err := tx.WithContext(ctx).Table(tb.TableName()).Where("crowd_funding_id = ? AND crowd_funding_thali_id = ? AND product_sku_id = ? AND product_id = ?", CrowdFundingId, CrowdFundingThaliId, ProductSkuId, ProductId).First(&result).Error; err != nil {
			return err
		}

		global.Log.Info("查询完毕", zap.Any("data", result))

		tb2 := model.PmsSkuStock{}
		skuUpdates := make(map[string]interface{})
		skuUpdates["lock_stock"] = result.SkuLockStock - num
		if err := tx.WithContext(ctx).Table(tb2.TableName()).Where("id = ?", ProductSkuId).Updates(skuUpdates).Error; err != nil {
			return err
		}

		global.Log.Info("商品表库存更新")

		tb3 := model.SmsCrowdFundingThali{}
		crowdFundingUpdates := make(map[string]interface{})
		crowdFundingUpdates["lock_thali_count"] = result.CftLockThaliCount - num
		if err := tx.WithContext(ctx).Table(tb3.TableName()).Where("id = ?", CrowdFundingThaliId).Updates(crowdFundingUpdates).Error; err != nil {
			return err
		}

		global.Log.Info("众筹表库存更新")

		// 返回 nil 提交事务
		return nil
	}); err != nil {
		return err
	}
	return nil
}
